
if exists (select 1 from sysobjects where name = 'set_schemas' and type = 'P')
begin
   drop procedure set_schemas
   print 'Procedure: set_schemas deleted ...'
end
go
-- setzt anzahl tage pro monat fest
create procedure set_schemas(
  @name            varchar(32) = 'oktober',
  @mandid          char(2)     = 'li'
)
as
begin

  set nocount on

  insert into Schemas (Name, Status, mandid) values (@name, 1, @mandid)
  
  select schemaid = s.SchemaID 
    from Schemas s
   where s.Name = @name 
     and s.Status = 1 
     and s.SchemaID = (select max(s2.SchemaID) from Schemas s2 where s2.Name = s.Name and s2.Status = 1 and s2.mandid = @mandid)
     and s.mandid = @mandid

end
go

print 'Procedure: set_schemas done ...'
go

grant exec on set_schemas to prsadmins with grant option
go
grant exec on set_schemas to prsusers
go

